$(function () {
    //常用jQuery对象
    var dictionaryItem_datagrid = $("#dictionaryItem_datagrid");
    var entity_item_dialog = $("#entity_item_dialog");
    var entity_item_form = $("#entity_item_form");


    //==========================================================================
    //封装方法
    var itemMethodObj = {

        //添加按钮方法
        itemadd: function () {
            //字典目录对象
            var getDictionaryRow = $("#dictionary_datagrid").datagrid("getSelected");
            if (!getDictionaryRow) {
                $.messager.alert("温馨提示", "亲!请在字典目录选择一条目录", "info");
                return;
            }
            entity_item_dialog.dialog("open");
            entity_item_dialog.dialog("setTitle", "新增");
            var itemName ={
                "parent.name": getDictionaryRow.name
            }

            entity_item_dialog.form("load", itemName);
        },

        //编辑按钮方法
        itemEdit: function () {
            //字典目录对象
            var getDictionaryRow = $("#dictionary_datagrid").datagrid("getSelected");
            if (!getDictionaryRow) {
                $.messager.alert("温馨提示", "亲!请在字典目录选择一条目录", "info");
                return;
            }
            var itemRow = dictionaryItem_datagrid.datagrid("getSelected");
            if (!itemRow) {
                $.messager.alert("温馨提示", "亲!请选择一条明细", "info");
                return;
            }
            entity_item_dialog.dialog("open");
            entity_item_dialog.dialog("setTitle", "编辑");
            itemRow["parent.name"] = itemRow.parent.name;
            entity_item_dialog.form("load", itemRow);
        },


        //编辑或新增方法具体逻辑
        saveOrUpdate: function () {
            entity_item_form.form("submit", {
                url: "/systemDictionaryItem/saveOrUpdate.do",
                onSubmit: function (param) {
                    //字典目录对象
                    var getDictionaryRow = $("#dictionary_datagrid").datagrid("getSelected");
                    param["parent.id"] =  getDictionaryRow.id;
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            entity_item_dialog.dialog("close");
                            dictionaryItem_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            entity_item_dialog.dialog("close");
                        });
                    }
                }
            })
        },


        //删除字典明细
        itemDelete: function () {
            var row = dictionaryItem_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            $.messager.confirm('温馨提示',"请问确定要删除此字典明细吗?", function (r) {
                if (r) {
                    $.get("/systemDictionaryItem/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                dictionaryItem_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }, "json")
                }
            });

        },

        //刷新页面
        itemReloadList: function () {
            dictionaryItem_datagrid.datagrid("reload");
        }

    }


    //==========================================================================================
    //按钮绑定事件
    $("[data-itemcmd]").click(function () {
        var itemcmd = $(this).data("itemcmd");
        itemMethodObj[itemcmd]();
    });

    //页面数据加载
    dictionaryItem_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        rownumbers: true,
        columns: [[
            {field: 'name', title: '字典明细编码',width:50, align: "center"},
            {field: 'intro', title: '字典明细名称',width:50, align: "center"},
            {field: 'parent', title: '字典明细简介',width:50, align: "center", formatter: function (parent) {
                    return parent ? parent.name :parent;
                }}
        ]],
        toolbar: "#itemTools"
    });


    //编辑添加弹出框设置
    entity_item_dialog.dialog({
        width: 350,
        height: 200,
        closed: true,
        onClose:function () {
            entity_item_dialog.form("clear");
        },
        buttons: [{
            text: '保存',
            iconCls: "icon-save",
            handler: itemMethodObj["saveOrUpdate"]
        }, {
            iconCls: "icon-cancel",
            text: '取消',
            handler: function () {
                entity_item_dialog.dialog("close");
            }
        }]
    });

    //form表单设置
    entity_item_dialog.form({
        fit: true
    });

});

